//
//  TotalClassifyViewController.m
//  PeopleStreet
//
//  Created by 喻飞 on 15/12/28.
//  Copyright © 2015年 yuf. All rights reserved.
//

#import "TotalClassifyViewController.h"
#import "MultilevelMenu.h"
#import "ShoppingProductListViewController.h"
@interface TotalClassifyViewController ()
@property(nonatomic,strong)NSMutableArray * categoryOne;
@property(nonatomic,strong)NSMutableArray * categoryTwo;
@property(nonatomic,strong)NSMutableArray * categoryThree;
@property(nonatomic,assign)NSInteger categoryIndex;
@property(nonatomic,strong)NSString * CateIdOne;
@property(nonatomic,strong)NSString * CateIdTwo;
@property(nonatomic,strong)NSString * CateIdThree;
@property(nonatomic,assign)NSInteger scroIndex;
@end

@implementation TotalClassifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navgationSearchView.layer.cornerRadius = 17;
    self.navgationSearchView.layer.borderWidth = 1;
    self.navgationSearchView.layer.borderColor = kGetColor(230, 230, 230).CGColor;
    
    _categoryOne = [NSMutableArray array];
     _categoryTwo = [NSMutableArray array];
     _categoryThree = [NSMutableArray array];
    [self initViewLayout];
    _categoryIndex = 0;
    _CateIdOne = @"";
    [self getGoodsCategory];
}

- (void)initCategoryView{
    NSMutableArray *lis=[NSMutableArray arrayWithCapacity:0];
    NSMutableArray *classifyArr = [_categoryOne mutableCopy];
    NSMutableArray *classifyImageArr = [NSMutableArray arrayWithObjects:@"beauty-makeup",@"JEWELRY",@"clothes",@"ShoesBag",@"exercise",@"kid",@"Furniture",@"digital", @"supermark",nil];
    /**
     *  构建需要数据 2层或者3层数据 (ps 2层也当作3层来处理)
     */
    for (int i=0; i<classifyImageArr.count; i++) {
        
        rightMeun * meun=[[rightMeun alloc] init];
        meun.meunName=classifyArr[i][@"mgcaName"];
        meun.imageName =classifyImageArr[i];
        NSMutableArray * sub=[NSMutableArray arrayWithCapacity:0];
        for ( int j=0; j <_categoryTwo.count; j++) {
            
            rightMeun * meun1=[[rightMeun alloc] init];
            //            meun1.meunName=[NSString stringWithFormat:@"%d头菜单%d",i,j];
            meun1.meunName=_categoryTwo[j][@"mgcaName"];
            [sub addObject:meun1];
            
            //meun.meunNumber=2;
            
            NSMutableArray *zList=[NSMutableArray arrayWithCapacity:0];
            //            if (j%2==0) {
            NSArray * array =_categoryTwo[j][@"categoryList"];
            for ( int z=0; z <array.count; z++) {
                
                rightMeun * meun2=[[rightMeun alloc] init];
                //                    meun2.meunName=[NSString stringWithFormat:@"%d层菜单%d",j,z];
                meun2.meunName=array[z][@"mgcaName"];
                [zList addObject:meun2];
                
            }
            //            }
            
            meun1.nextArray=zList;
        }
        
        
        meun.nextArray=sub;
        [lis addObject:meun];
    }
    
    /**
     *  适配 ios 7 和ios 8 的 坐标系问题
     */
    self.automaticallyAdjustsScrollViewInsets=NO;
    
    /**
     默认是 选中第一行
     
     :returns: <#return value description#>
     */

    MultilevelMenu * view=[[MultilevelMenu alloc] initWithFrame:flexibleFrame(CGRectMake(0, 64, 320, kDeviceHeight-64), NO) WithData:lis withSelectIndex:^(NSInteger left, NSInteger right,NSInteger index,rightMeun* info) {
        if (info) {
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
            ShoppingProductListViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"ShoppingProductList"];
            page.CategoryId = _categoryTwo[right][@"categoryList"][index][@"mgcaId"];
            [self.navigationController   pushViewController:page animated:YES];
        }else{
            _categoryIndex = 1;
             _CateIdTwo = _categoryOne[left][@"mgcaId"];
            [self getGoodsCategory];
           
            _scroIndex = left;
        }
        
    }];
    
    
    view.needToScorllerIndex=_scroIndex;
    
    view.isRecordLastScroll=YES;
    [self.view addSubview:view];

    //滚动搜索类型
    ScroTitleView * scroTitle = [[ScroTitleView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 0, 50, 35), NO) Titles:@[@"商品",@"品牌",@"店铺"] complect:^(NSInteger index) {
        
    }];
    [self.navgationSearchView addSubview:scroTitle];


}

#pragma mark  布局调整--=========================================
- (void)initViewLayout{
    //功能栏
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navgationView];
    [self adaptiveViewLayout:self.navgationSearchView];
}
#pragma mark  返回--=========================================
- (IBAction)backClick:(UIButton *)sender {
    [self.navigationController  popViewControllerAnimated:YES];
}


- (void)getGoodsCategory{
    
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:@"queryMallGoodsCategory" forKey:@"method"];
    
    if (_categoryIndex==0) {

    [dic setObject:_CateIdOne forKey:@"mgcaParentId"];
        self.shareApi.delegate=self;
        [self.shareApi  requestOperationWithDic:dic withPath:SX_GoodsCategory withHttpType:@"POST" withTag:SX_GetGoodsCategory];
    }else if(_categoryIndex == 1){
        if (!_CateIdTwo) {
            _CateIdTwo = _categoryOne[0][@"mgcaId"];
        }
        [dic setObject:_CateIdTwo forKey:@"mgcaParentId"];
        self.shareApi.delegate=self;
        [self.shareApi  requestOperationWithDic:dic withPath:SX_queryMallTwoGoodsCategory withHttpType:@"POST" withTag:SX_queryMallTwoGoodsCategoryTag];
       
    }
//    else{
//        [_categoryThree removeAllObjects];
//        for (NSInteger i = 0; i<_categoryTwo.count; i++) {
//            _CateIdThree = _categoryTwo[i][@"mgcaId"];
//            [dic setObject:_CateIdThree forKey:@"mgcaParentId"];
//            self.shareApi.delegate=self;
//            [self.shareApi  requestOperationWithDic:dic withPath:SX_GoodsCategory withHttpType:@"POST" withTag:SX_GetGoodsCategory];
//        }
//    }
    
   
    

}



#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    
    
    if (tag==SX_GetGoodsCategory) {
        
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            
            if (_categoryIndex == 0) {
                _categoryOne = database[@"result"];
                _categoryIndex = 1;
                [self getGoodsCategory];
            }else if (_categoryIndex == 1){
                _categoryTwo = database[@"result"];
                _categoryIndex = 2;
                [self getGoodsCategory];
            }else{
                NSArray * array = database[@"result"];
            [_categoryThree addObject:array] ;
                if (_categoryThree.count == _categoryTwo.count) {
                    [self initCategoryView];
                }

            }
            
        }
        
    }else if (tag == SX_queryMallTwoGoodsCategoryTag){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            if (_categoryIndex == 1){
                _categoryTwo = database[@"result"];
                _categoryIndex = 2;
                [self initCategoryView];
//                [self getGoodsCategory];
//                NSArray * array = database[@"result"];
//                [_categoryThree addObject:array] ;
//                if (_categoryThree.count == _categoryTwo.count) {
//                    [self initCategoryView];
//                }
            }
            
        }
    }
}

-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    //
    [self dismissWaitDialog];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
